package sis.studentinfo;

import java.math.BigDecimal;

import com.jimbob.ach.Ach;
import com.jimbob.ach.AchCredentials;
import com.jimbob.ach.AchResponse;
import com.jimbob.ach.AchTransactionData;

public class Account implements Accountable{

	public enum BankAccountType {
		CHECKING("ck"), SAVINGS("sv");
		
		private String value;
		private BankAccountType(String value) {
			this.value = value;
		}
		
		@Override
		public String toString() {
			return this.value;
		}
	}
	
	private BigDecimal balance = new BigDecimal("0.00");
	private int transactionCount;
	private String bankAba;
	private BankAccountType bankAccountType;
	private String bankAccountNumber;
	private Ach ach;
	
	
	public void credit(BigDecimal amount) {
		balance = balance.add(amount);
		transactionCount++;
	}
	
	public BigDecimal getBalance() {
		return this.balance;
	}

	public BigDecimal transactionAverage() {
		return balance.divide(new BigDecimal(transactionCount), BigDecimal.ROUND_HALF_UP);
	}

	public void setBankAba(String aba) {
		this.bankAba = aba;
	}

	public void setBankAccountType(BankAccountType bankAccountType) {
		this.bankAccountType = bankAccountType;
	}

	public void setBankAccountNumber(String accountNumber) {
		this.bankAccountNumber = accountNumber;
	}

	public void setAch(Ach ach) {
		this.ach = ach;
	}

	@SuppressWarnings("unused")
	public void transferFromBank(BigDecimal amount) {
		AchCredentials credentials = createCredentials();
		
		AchTransactionData data = createData(amount);
		
		Ach ach = getAch();
		AchResponse achResponse = ach.issueDebit(credentials, data);
		
		credit(amount);
		
	}

	private Ach getAch() {
		return this.ach;
	}

	private AchTransactionData createData(BigDecimal amount) {
		AchTransactionData data = new AchTransactionData();
		data.description = "transfer from bank";
		data.amount = amount;
		data.aba = bankAba;
		data.account = bankAccountNumber;
		data.accountType = bankAccountType.toString();
		return data;
	}

	private AchCredentials createCredentials() {
		AchCredentials credentials = new AchCredentials();
		credentials.merchantId = "12355";
		credentials.userName = "sismerc1920";
		credentials.password = "pitseleh411";
		return credentials;
	}

}
